package {{.table}}

import (
	"sync"
)

type {{.schema.Opts.UpperCamelCaseTable}}Service struct {
	Dao *{{.schema.Opts.UpperCamelCaseTable}}Dao
}

var (
	instance{{.schema.Opts.UpperCamelCaseTable}}Service *{{.schema.Opts.UpperCamelCaseTable}}Service
	once{{.schema.Opts.UpperCamelCaseTable}}Service     sync.Once
)

func New{{.schema.Opts.UpperCamelCaseTable}}Service() *{{.schema.Opts.UpperCamelCaseTable}}Service {
	once{{.schema.Opts.UpperCamelCaseTable}}Service.Do(func() {
		instance{{.schema.Opts.UpperCamelCaseTable}}Service = &{{.schema.Opts.UpperCamelCaseTable}}Service{Dao: New{{.schema.Opts.UpperCamelCaseTable}}Dao()}
	})
	return instance{{.schema.Opts.UpperCamelCaseTable}}Service
}

// 创建 {{.schema.Opts.TableComment}}
func ({{.table}} *{{.schema.Opts.UpperCamelCaseTable}}Service) Create{{.schema.Opts.UpperTable}}(model *{{.schema.Opts.UpperCamelCaseTable}}) (*{{.schema.Opts.UpperCamelCaseTable}}, error) {
	id, err := {{.table}}.Dao.Create{{.schema.Opts.UpperCamelCaseTable}}(model)
	if err != nil {
		return &{{.schema.Opts.UpperCamelCaseTable}}{}, err
	}
	model.Id = id
	return model, nil
}

// 根据 id 删除 {{.schema.Opts.TableComment}}
func ({{.table}} *{{.schema.Opts.UpperCamelCaseTable}}Service) Delete{{.schema.Opts.UpperTable}}ById(id *{{.schema.Opts.UpperCamelCaseTable}}Id) error {
	if err := {{.table}}.Dao.Delete{{.schema.Opts.UpperCamelCaseTable}}ById(id); err != nil {
		return err
	}

	return nil
}

// 根据 id 修改 {{.schema.Opts.TableComment}}
func ({{.table}} *{{.schema.Opts.UpperCamelCaseTable}}Service) Update{{.schema.Opts.UpperTable}}ById(id *{{.schema.Opts.UpperCamelCaseTable}}Id, model *{{.schema.Opts.UpperCamelCaseTable}}) error {
	if err := {{.table}}.Dao.Update{{.schema.Opts.UpperCamelCaseTable}}ById(id, model); err != nil {
		return err
	}

	return nil
}

// 查询全部 {{.schema.Opts.TableComment}}
func ({{.table}} *{{.schema.Opts.UpperCamelCaseTable}}Service) FindAll{{.schema.Opts.UpperTable}}() (*[]{{.schema.Opts.UpperCamelCaseTable}}, error) {
	return {{.table}}.Dao.FindAll{{.schema.Opts.UpperCamelCaseTable}}()
}
